package com.api.gbhg.daiyanlu.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.daiyanlu.Dao.BudgetImplementationRateDao;
import com.api.gbhg.liuwenbo.pojo.Info;
import com.api.gbhg.liuwenbo.utils.AlgorithmUtil;
import weaver.conn.RecordSet;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 *非独立核算单位财务预算执行率
 * Mr.dyl
 */
public class BudgetImplementationRateActionApi {
    private Info[] paIf;
    private String nd;
    private String dw;

    public BudgetImplementationRateActionApi() {
    }
    @POST
    @Path("/getRate")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getData(Map<String, Object> param) {
        this.init(param);
        this.setImplementRate();
        return JSON.toJSONString(this.paIf);
    }

    private void init(Map<String, Object> param) {
        this.paIf = new Info[1];
        this.setNd(param);
        this.setDw(param);
    }

    public void setDw(Map<String, Object> param){
        if(param.containsKey("dw") && param.get("dw") instanceof Integer ){
            this.dw = AlgorithmUtil.getSubCompanIdBybmId((Integer) param.get("dw"));
        }else{
            this.dw = "2"; //默认为2(拱北海关)
        }
    }

    public void setNd(Map<String, Object> param) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        if (param.containsKey("nd") && param.get("nd") instanceof Integer) {
            this.nd = param.get("nd").toString();
        } else {
            Date date = new Date();
            this.nd = sdf.format(date.getTime());
        }

    }


    /**
     * 给预算执行率赋值
     * @return
     */
    public void setImplementRate(){
        RecordSet rs = BudgetImplementationRateDao.getTotalImplement(nd,dw,0);
        String RealTime = "0.00";
        String YearAvg = "0.00";
        while(rs.next()){
            RealTime = AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"), rs.getDouble("ze"));
            YearAvg = AlgorithmUtil.getYearAvgImplRate(Integer.valueOf(nd),rs.getDouble("ze"));
        }
        Map<String, Object>[] map = new HashMap[2];
        this.paIf[0] = new Info();
        map[0] = new HashMap();
        map[0].put("content", "总执行率");
        map[0].put("zxl1", RealTime);
        map[0].put("zxl2", YearAvg);
        map[0].put("state", "down");
        map[1] = new HashMap();
        map[1].put("content", "当年执行率");
        map[1].put("zxl1", RealTime);
        map[1].put("zxl2", YearAvg);
        map[1].put("state", "up");
        this.paIf[0].setTitle("预算执行率");
        this.paIf[0].setData(map);
    }
}
